package de.ifi.lmu.snitzelhunt.view;

import java.util.ArrayList;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.LatLngBounds.Builder;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import de.ifi.lmu.snitzelhunt.R;
import de.ifi.lmu.snitzelhunt.basics.Task;
import de.ifi.lmu.snitzelhunt.creator.GatherRouteData;
import de.ifi.lmu.snitzelhunt.util.GSonUtility;

/**
 * @author Sarah Torma
 * 
 *         This class is used to give an overview of all tasks and all GPS data
 *         for the whole route in form of a google map
 * 
 */
public class Activity_ShowRoute extends ActionBarActivity implements
		GooglePlayServicesClient.ConnectionCallbacks,
		GooglePlayServicesClient.OnConnectionFailedListener,
		OnMapLoadedCallback {

	GoogleMap gm;
	LocationClient loCli;
	GatherRouteData grd;
	LatLng userPos;
	LatLngBounds.Builder llb = new Builder();
	PolylineOptions rectOptions = new PolylineOptions();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_route);

		loCli = new LocationClient(this, this, this);
		Log.i("LocationClient", "instantiated");

	}

	private void setUpMap() {
		if (gm == null) {
			gm = ((SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.fragment_show_route_map)).getMap();
			if (gm != null) {
				configureMap();
			}
		} else {
			gm.clear();
			configureMap();
		}
	}

	private void configureMap() {

		Location current = loCli.getLastLocation();
		//userPos = new LatLng(current.getLatitude(), current.getLongitude());
		//gm.moveCamera(CameraUpdateFactory.newLatLngZoom(userPos, 10));

		readPackageContents();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.show_route, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		int id = item.getItemId();
		if (id == R.id.action_settings) {

			Intent howto = new Intent(this, Activity_HowTo.class);
			startActivity(howto);
		}

		return true;
	}

	private void readPackageContents() {
		ArrayList<LatLng> waypoints = new ArrayList<LatLng>();

		Intent intent = this.getIntent();
		String json = intent.getStringExtra("taskList");
		ArrayList<Task> tasks = GSonUtility.deserializeTaskList(json);

		gm.clear();

		for (Task t : tasks) {
			LatLng temp = new LatLng(t.getLatitude(), t.getLongitude());
			waypoints.add(temp);
			llb.include(temp);
			rectOptions.add(temp);
			
			if(tasks.get(tasks.size()-1).equals(t))
			{
				gm.addMarker(new MarkerOptions().position(temp).icon(
						BitmapDescriptorFactory
								.fromResource((R.drawable.flag_ziel))));
			}
			else
				{gm.addMarker(new MarkerOptions().position(temp).icon(
					BitmapDescriptorFactory
							.fromResource((R.drawable.flag_small))));}
		}

		gm.setOnMapLoadedCallback(this);
		Log.i("showRoute", "found " + waypoints.size() + " waypoints");

	}

	@Override
	public void onConnectionFailed(ConnectionResult arg0) {
		Log.e("locationClient", "connect failed: " + arg0.toString());
	}

	@Override
	public void onConnected(Bundle arg0) {
		setUpMap();
	}

	@Override
	public void onDisconnected() {
		Toast.makeText(this, "LocatinClient Disconnected", Toast.LENGTH_SHORT)
				.show();
	}

	@Override
	protected void onStart() {
		super.onStart();
		loCli.connect();
		Toast.makeText(this, "connecting LocationClient", Toast.LENGTH_SHORT)
				.show();
	}

	@Override
	protected void onStop() {
		super.onStop();
		loCli.disconnect();
	}

	@Override
	public void onMapLoaded() {
		CameraUpdate cup = CameraUpdateFactory.newLatLngBounds(llb.build(), 100);
		gm.animateCamera(cup);
		gm.addPolyline(rectOptions);
	}
}
